<template>
    <el-form ref="ruleFormRef" class="edit-form" :model="ruleForm" :rules="rules" label-width="auto" :size="formSize" status-icon >
        <el-form-item label="证书名称" prop="title">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="证书编号" prop="numbers">
            <el-input v-model="ruleForm.numbers" />
        </el-form-item>
        <el-form-item label="生效日期" prop="start_date">
            <el-date-picker v-model="ruleForm.start_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="失效日期" prop="end_date">
            <el-date-picker v-model="ruleForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%;" />
        </el-form-item>
        <el-form-item label="证书文件" prop="expire">
            <el-row :gutter="20">
                <el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
                <el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
                <el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
                <el-col :span="6"><div class="grid-content ep-bg-purple" /></el-col>
            </el-row>
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef,true)" :loading="loading"> 保存并返回 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import { useRouter } from 'vue-router';
import http from '../../../util/request';
import router from '../../../router';
const loading = ref<boolean>(false)
const id:any = ref(useRouter().currentRoute.value.params.id)

interface RuleForm {
    id: number
    title: string
    numbers: string
    type: string
    start_date: string
    end_date: string
    files: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    title: '',
    numbers:'',
    files: '',
    type: '',
    start_date: '',
    end_date: '',
})


const getData = () => {
    if(id.value == 0){
        return;
    }
    http.get('/company/'+id.value).then((response:any)=>{
        ruleForm.value = response.data.data
    })
}

const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入名称', trigger: 'blur' },
        { min: 2, max: 30, message: '范围在2-30字之间', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined,type:boolean) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            // loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/company',ruleForm.value)
            }else{
                request = http.put('/company',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);return
                }
                ElMessage.success(response.data.msg);
                if(type){
                    router.push({ name: 'company'})
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

onMounted(()=>{
    getData()
})
</script>
<style scoped>
</style>
